package com.lagou.service.impl;

import com.lagou.dao.AccountDao;
import com.lagou.pojo.Account;
import com.lagou.service.TransferService;

/**
 * @author 应癫
 */
public class TransferServiceImpl implements TransferService {

    private AccountDao accountDao;
//    private TransactionManager transactionManager;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    //事务处理(横切逻辑)交由代理对象处理，此处不再注入TransactionManager
//    public void setTransactionManager(TransactionManager transactionManager) {
//        this.transactionManager = transactionManager;
//    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
//        try {
//            // 开启事务(关闭事务的自动提交)
//            transactionManager.beginTransaction();

            //业务逻辑
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(from);
//            int c = 1/0;
            accountDao.updateAccountByCardNo(to);

//            // 提交事务
//            transactionManager.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            // 回滚事务
//            transactionManager.rollback();
//            throw e;
//        }
    }

}
